package io.renren.modules.department.service.impl;

import io.renren.common.exception.RRException;
import io.renren.modules.department.mapper.DepartmentMapper;
import io.renren.modules.department.model.DepartmentBo;
import io.renren.modules.department.model.DepartmentEntity;
import io.renren.modules.department.model.DepartmentVo;
import io.renren.modules.department.service.DepartmentService;
import io.renren.modules.sys.entity.SysMenuEntity;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


@Service("departmentService")
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    /**
     * 新增部门信息
     *
     * @param departmentBo
     * @return
     */
    @Override
    public Integer save(DepartmentBo departmentBo) {
        //判断部门名称是否存在，所有节点都判断
        DepartmentEntity departmentName = departmentMapper.checkDepartmentName(departmentBo.getDepartmentName());
        if (null != departmentName) {
            throw new RRException("部门名称已存在", HttpStatus.UNAUTHORIZED.value());
        }
        DepartmentEntity departmentEntity = new DepartmentEntity();
        BeanUtils.copyProperties(departmentBo, departmentEntity);
        departmentEntity.setCreateTime(new Date());
        return departmentMapper.save(departmentEntity);
    }

    /**
     * 修改部门信息
     *
     * @param departmentBo
     * @return
     */
    @Override
    public Integer updateById(DepartmentBo departmentBo) {
        DepartmentEntity departmentById = departmentMapper.getDepartmentById(departmentBo.getId());
        //判断部门名称是否存在，所有节点都判断
        DepartmentEntity departmentName = departmentMapper.checkDepartmentName(departmentBo.getDepartmentName());
        if (null != departmentName && !(departmentById.getDepartmentName().equals(departmentName.getDepartmentName()))) {
            throw new RRException("部门名称已存在", HttpStatus.UNAUTHORIZED.value());
        }
        DepartmentEntity departmentEntity = new DepartmentEntity();
        BeanUtils.copyProperties(departmentBo, departmentEntity);
        return departmentMapper.updateById(departmentEntity);
    }

    /**
     * 根据id删除部门信息
     *
     * @param idList
     * @return
     */
    @Override
    public Integer removeByIds(List<Integer> idList) {
        return departmentMapper.removeByIds(idList);
    }

    @Override
    public List<DepartmentEntity> queryListParentId(Long parentId) {

        return departmentMapper.queryListParentId(parentId);
    }

    /**
     * 遍历所有部门
     *
     * @return
     */
    @Override
    public List<DepartmentVo> queryList() {
        //List<DepartmentVo> departmentVoList = departmentMapper.getNodeTree();
        List<DepartmentVo> departmentVoList = departmentMapper.getAllList();
        return departmentVoList;
    }

    /**
     * 根据主键id查询部门信息
     *
     * @param id
     * @return
     */
    @Override
    public DepartmentEntity getById(Integer id) {
        DepartmentEntity departmentEntity = departmentMapper.getDepartmentById(id);
        return departmentEntity;
    }
}